package org.darcy.service.business.impl;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.darcy.dao.business.BusinessFinancialBillMapper;
import org.darcy.entity.business.BankInfo;
import org.darcy.entity.business.BusinessContract;
import org.darcy.entity.business.BusinessFinancialBill;
import org.darcy.entity.work.WorkInstance;
import org.darcy.framework.constant.SysConstant;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.service.business.BusinessClientService;
import org.darcy.service.business.BusinessContractService;
import org.darcy.service.business.BusinessFinancialBillService;
import org.darcy.service.work.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

/**
 * BusinessFinancialBill服务实现
 */
@Service
@Transactional
public class BusinessFinancialBillServiceImpl extends GenericServiceImpl<BusinessFinancialBill>
		implements BusinessFinancialBillService {

	@Autowired
	private BusinessFinancialBillMapper mapper;

	@Autowired
	private WorkService WorkService;

	@Autowired
	private BusinessContractService BusinessContractService;
	
	@Autowired
	@Lazy
	private BusinessClientService BusinessClientService;
	
	

	/**
	 * 处理前端数据
	 */
	private void processData(BusinessFinancialBill record) {

	}

	/**
	 * 处理工作流数据：主生成审批任务
	 */
	private void processWork(BusinessFinancialBill record) {
		BusinessContract data = this.getDetail(record.getId()).getContract();
		String title = String.format("[%s]的申请开票", data.getProjectOperatorName());
		String summary = String.format("合同编号：%s\n委托单位：%s\n委托业务：%s\n经办人：%s",data.getFormNo(), data.getClientName(), data.getProjectType(),
				data.getProjectOperatorName());
		WorkService.addWorkTask(record, title, summary, SysConstant.FormType.FinancialBill);
	}

	@Override
	public BusinessFinancialBill insert(BusinessFinancialBill record) {
		record.setCreateTime(new Date());
		// 合同编号处理
		String time = DateUtil.format(record.getCreateTime(), DatePattern.PURE_DATETIME_FORMATTER);
		record.setFormNo("SQKP" + time);
		processData(record);
		super.insert(record);
		processWork(record);
		return record;
	}

	@Override
	public BusinessFinancialBill update(BusinessFinancialBill record) {
		processData(record);
		super.update(record);
		processWork(record);
		return record;
	}

	@Override
	public BusinessFinancialBill getDetail(Integer id) {
		BusinessFinancialBill record = mapper.selectById(id);
		record.setContract(BusinessContractService.getDetail(record.getContractId()));
		if(record.getReceiptCompany()!=null) {
			BusinessContract BusinessContract = BusinessContractService.getById(record.getContractId());
			List<BankInfo> bankInfoList = BusinessClientService.getDetail(BusinessContract.getClientId()).getBankInfoList();
			Optional<BankInfo> optional = bankInfoList.stream().filter(a->a.getId().equals(record.getReceiptCompany())).findFirst();
			if(optional.isPresent()) {
				record.setBankInfo(optional.get());
			}
		}
		return record;
	}
	
	@Override
	public int delete(Object id) {
		super.delete(id);
		WorkInstance param = WorkInstance.builder().formId((Integer) id).formType(SysConstant.FormType.FinancialBill)
				.build();
		WorkInstance work = WorkService.getBusinessWork(param);
		WorkService.delete(work.getId());
		return 1;
	}

	@Override
	public PageData<BusinessFinancialBill> page(PageParam param) {
		PageData<BusinessFinancialBill> data = super.getPageData(param);
		return data;
	}

	@Override
	public List<BusinessFinancialBill> getList(Integer contractId) {
		QueryWrapper<BusinessFinancialBill> q = new QueryWrapper<BusinessFinancialBill>();
		q.eq("contract_id", contractId);
		return this.getList(q);
	}

}
